package com.tencent.bugly.proguard;

import android.content.Context;
import android.os.Process;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/* compiled from: BUGLY */
public final class y {
    public static boolean a = true;
    private static boolean b = true;
    private static SimpleDateFormat c;
    private static int d = 30720;
    private static StringBuilder e;
    private static StringBuilder f;
    private static boolean g;
    private static a h;
    private static String i;
    private static String j;
    private static Context k;
    private static String l;
    private static boolean m;
    private static boolean n = false;
    private static ExecutorService o;
    private static int p;
    private static final Object q = new Object();

    /* compiled from: BUGLY */
    public static class a {
        private boolean a;
        private File b;
        private String c;
        private long d;
        private long e = 30720;

        public a(String str) {
            if (str != null && !str.equals("")) {
                this.c = str;
                this.a = a();
            }
        }

        private boolean a() {
            try {
                this.b = new File(this.c);
                if (this.b.exists() && !this.b.delete()) {
                    this.a = false;
                    return false;
                } else if (this.b.createNewFile()) {
                    return true;
                } else {
                    this.a = false;
                    return false;
                }
            } catch (Throwable th) {
                x.a(th);
                this.a = false;
                return false;
            }
        }

        public final boolean a(String str) {
            FileOutputStream fileOutputStream;
            Throwable th;
            Throwable th2;
            if (!this.a) {
                return false;
            }
            try {
                fileOutputStream = new FileOutputStream(this.b, true);
                try {
                    byte[] bytes = str.getBytes("UTF-8");
                    fileOutputStream.write(bytes);
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    this.d += (long) bytes.length;
                    this.a = true;
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                    }
                    return true;
                } catch (Throwable th3) {
                    th = th3;
                    try {
                        x.a(th);
                        this.a = false;
                        if (fileOutputStream != null) {
                            return false;
                        }
                        try {
                            fileOutputStream.close();
                            return false;
                        } catch (IOException e2) {
                            return false;
                        }
                    } catch (Throwable th4) {
                        th2 = th4;
                        if (fileOutputStream != null) {
                            try {
                                fileOutputStream.close();
                            } catch (IOException e3) {
                            }
                        }
                        throw th2;
                    }
                }
            } catch (Throwable th5) {
                th2 = th5;
                fileOutputStream = null;
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                throw th2;
            }
        }
    }

    static {
        c = null;
        try {
            c = new SimpleDateFormat("MM-dd HH:mm:ss");
        } catch (Throwable th) {
            x.b(th.getCause());
        }
    }

    public static synchronized void a(Context context) {
        synchronized (y.class) {
            if (!(m || context == null || !a)) {
                try {
                    o = Executors.newSingleThreadExecutor();
                    f = new StringBuilder(0);
                    e = new StringBuilder(0);
                    k = context;
                    com.tencent.bugly.crashreport.common.info.a a = com.tencent.bugly.crashreport.common.info.a.a(context);
                    i = a.d;
                    a.getClass();
                    j = "";
                    l = k.getFilesDir().getPath() + "/buglylog_" + i + "_" + j + ".txt";
                    p = Process.myPid();
                } catch (Throwable th) {
                }
                m = true;
            }
        }
    }

    public static void a(int i) {
        synchronized (q) {
            d = i;
            if (i < 0) {
                d = 0;
            } else if (i > 30720) {
                d = 30720;
            }
        }
    }

    public static void a(String str, String str2, Throwable th) {
        if (th != null) {
            String message = th.getMessage();
            if (message == null) {
                message = "";
            }
            a(str, str2, message + '\n' + z.b(th));
        }
    }

    public static synchronized void a(final String str, final String str2, final String str3) {
        synchronized (y.class) {
            if (m && a) {
                try {
                    o.execute(new Runnable() {
                        public final void run() {
                            y.c(str, str2, str3);
                        }
                    });
                } catch (Throwable e) {
                    x.b(e);
                }
            }
        }
    }

    private static synchronized void c(String str, String str2, String str3) {
        synchronized (y.class) {
            if (b) {
                d(str, str2, str3);
            } else {
                e(str, str2, str3);
            }
        }
    }

    private static synchronized void d(String str, String str2, String str3) {
        synchronized (y.class) {
            String a = a(str, str2, str3, (long) Process.myTid());
            synchronized (q) {
                try {
                    f.append(a);
                    if (f.length() >= d) {
                        f = f.delete(0, f.indexOf("\u0001\r\n") + 1);
                    }
                } catch (Throwable th) {
                    if (!x.b(th)) {
                        th.printStackTrace();
                    }
                }
            }
        }
    }

    private static synchronized void e(String str, String str2, String str3) {
        synchronized (y.class) {
            String a = a(str, str2, str3, (long) Process.myTid());
            synchronized (q) {
                try {
                    f.append(a);
                    if (f.length() <= d) {
                    } else if (g) {
                    } else {
                        g = true;
                        if (h == null) {
                            h = new a(l);
                        } else if (h.b == null || h.b.length() + ((long) f.length()) > h.e) {
                            h.a();
                        }
                        if (h.a(f.toString())) {
                            f.setLength(0);
                            g = false;
                        }
                    }
                } catch (Throwable th) {
                }
            }
        }
    }

    private static String a(String str, String str2, String str3, long j) {
        String format;
        e.setLength(0);
        if (str3.length() > 30720) {
            str3 = str3.substring(str3.length() - 30720, str3.length() - 1);
        }
        Date date = new Date();
        if (c != null) {
            format = c.format(date);
        } else {
            format = date.toString();
        }
        e.append(format).append(" ").append(p).append(" ").append(j).append(" ").append(str).append(" ").append(str2).append(": ").append(str3).append("\u0001\r\n");
        return e.toString();
    }

    public static byte[] a() {
        if (!b) {
            return b();
        }
        if (a) {
            return z.a(null, f.toString(), "BuglyLog.txt");
        }
        return null;
    }

    private static byte[] b() {
        if (!a) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        synchronized (q) {
            if (h != null && h.a && h.b != null && h.b.length() > 0) {
                stringBuilder.append(z.a(h.b, 30720, true));
            }
            if (f != null && f.length() > 0) {
                stringBuilder.append(f.toString());
            }
        }
        return z.a(null, stringBuilder.toString(), "BuglyLog.txt");
    }
}
